﻿using LanguagePolish.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace LanguagePolish
{
    public static class Extentions
    {
        /// <summary>
        /// Преобразует день недели в целое число(0 - пн, 1 - вт, ...)
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static int ToInt(this DayOfWeek d)
        {
            int dayNum = (int)d;
            dayNum = dayNum == 0 ? 6 : dayNum - 1;
            return dayNum;
        }

        /// <summary>
        /// Вызывает метод OnPropertyChanged для всех public свойств данного BaseViewModel.
        /// </summary>
        /// <param name="vm"></param>
        public static void NotifyAll(this BaseViewModel vm)
        {
            Type type = vm.GetType();
            if (type.BaseType != typeof(BaseViewModel))
                type = type.BaseType;
            var properties = type.GetProperties();
            foreach (var prop in properties)
            {
                string name = prop.Name;
                vm.OnPropertyChanged(name);
            }
        }

        public static bool EqualWithEps(this double num1, double num2)
        { 
            const double eps = 0.0001;
            return Math.Abs(num2 - num1) < eps;
        }

        public static T FindAncestor<T>(this DependencyObject dependencyObject)
            where T : DependencyObject
        {
            var parent = VisualTreeHelper.GetParent(dependencyObject);
 
            if (parent == null) return null;
 
            var parentT = parent as T;
            return parentT ?? FindAncestor<T>(parent);
        }
    }
}
